package cn.java89.userservice.mybatis.service.impl;

//import cn.java89.userservice.mybatis.entity.Page;

import cn.java89.userservice.mybatis.entity.Student;
import cn.java89.userservice.mybatis.mapper.StudentMapper;
import cn.java89.userservice.mybatis.service.IStudentService;
import cn.java89.userservice.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@Service
public class StudentService extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Override
    public Student login(String phone, String pass) {
        //lambda查询
        StudentMapper mapper = getBaseMapper();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>();
        wrapper.eq(Student::getTelephone, phone).eq(Student::getPass, pass);
        return mapper.selectOne(wrapper);
    }

    @Override
    public cn.java89.userservice.mybatis.entity.Page search(int pageNo, int pageSize, String sname, String qq,
                                                            String phone, Date enterStart,
                                                            Date enterEnd) {
        StudentMapper mapper = getBaseMapper();
        QueryWrapper<Student> q = new QueryWrapper<>();
        q.like(StrUtil.isBlank(sname) == false, "sname", sname);
        q.like(StrUtil.isBlank(qq) == false, "qq", qq);
        q.like(StrUtil.isBlank(phone) == false, "telephone", phone);
        q.ge(StrUtil.isBlank(enterStart) == false, "enterdate", enterStart);
        q.le(StrUtil.isBlank(enterEnd) == false, "enterdate", enterEnd);
        IPage p = new Page(pageNo, pageSize);

        IPage<Student> page = mapper.selectPage(p, q);
        return new cn.java89.userservice.mybatis.entity.Page(page.getRecords(), pageNo, page.getTotal(), pageSize);
    }
}
